package cn.fscode.code.generator.service.impl;

import cn.fscode.code.generator.assembly.TemplateGroupAssembly;
import cn.fscode.code.generator.mapper.TemplateConfigMapper;
import cn.fscode.code.generator.model.po.TemplateConfigPO;
import cn.fscode.code.generator.model.po.TemplateGroupPO;
import cn.fscode.code.generator.model.req.TemplateGroupListReq;
import cn.fscode.code.generator.model.resp.TemplateGroupResp;
import cn.fscode.code.generator.repository.TemplateGroupRepository;
import cn.fscode.code.generator.service.TemplateGroupService;
import cn.fscode.commons.tool.core.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Service
@RequiredArgsConstructor
public class TemplateGroupServiceImpl implements TemplateGroupService {
    private final TemplateConfigMapper templateConfigMapper;
    private final TemplateGroupRepository templateGroupRepository;
    private final TemplateGroupAssembly templateGroupAssembly;
    @Override
    public List<TemplateGroupResp> listTemplateGroup(TemplateGroupListReq req) {
        List<TemplateGroupPO> templateGroupPOS = templateGroupRepository.listTemplateGroup(req);
        return templateGroupPOS.stream().map(templateGroupAssembly::from).collect(Collectors.toList());
    }

    @Override
    public TemplateGroupPO getById(Long id) {
        return templateGroupRepository.getById(id);
    }

    @Override
    public void save(TemplateGroupPO templateGroupPO) {
        TemplateGroupPO group = this.getByName(templateGroupPO.getGroupName());
        if (group != null) {
            throw new BizException(templateGroupPO.getGroupName() + " 已存在");
        }
        if (!templateGroupRepository.save(templateGroupPO)) {
            throw new BizException("保存失败");
        }
    }

    @Override
    public void updateById(TemplateGroupPO templateGroupPO) {
        if (!templateGroupRepository.updateById(templateGroupPO)) {
            throw new BizException("更新失败");
        }
    }

    @Override
    public void deleteById(TemplateGroupPO templateGroupPO) {
        if (!templateGroupRepository.removeById(templateGroupPO)) {
            throw new BizException("删除失败");
        }
    }

    @Override
    @Transactional
    public void deleteGroup(TemplateGroupPO templateGroupPO) {
        List<TemplateGroupPO> templateGroupPOS = templateGroupRepository.list();
        if (templateGroupPOS.size() == 1) {
            throw new BizException("无法删除，必须要有一个模板组");
        }
        if (!templateGroupRepository.removeById(templateGroupPO.getId())) {
            throw new BizException("模板组删除失败");
        }

        LambdaQueryWrapper<TemplateConfigPO> wrapper = Wrappers.<TemplateConfigPO>lambdaQuery()
                .eq(TemplateConfigPO::getGroupId, templateGroupPO.getId());
        if (templateConfigMapper.selectCount(wrapper) > 0) {
            throw new BizException("模板组下还含有模板文件, 不能删除");
        }
    }

    @Override
    public TemplateGroupPO getByName(String name) {
        LambdaQueryWrapper<TemplateGroupPO> wrapper = Wrappers.<TemplateGroupPO>lambdaQuery()
                .like(TemplateGroupPO::getGroupName, name);
        return templateGroupRepository.getOne(wrapper);
    }

    @Override
    public void deleteGroupBatch(List<Long> ids) {
        templateGroupRepository.removeByIds(ids);
    }
}
